/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.card.service.impl;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;
import top.continew.admin.card.mapper.UserWechatRelationMapper;
import top.continew.admin.card.model.entity.UserWechatRelationDO;
import top.continew.admin.card.model.query.UserWechatRelationQuery;
import top.continew.admin.card.model.req.UserWechatRelationReq;
import top.continew.admin.card.model.resp.UserWechatRelationDetailResp;
import top.continew.admin.card.model.resp.UserWechatRelationResp;
import top.continew.admin.card.service.UserWechatRelationService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 系统用户微信用户关系业务实现
 *
 * @author floyd
 * @since 2024/12/02 15:41
 */
@Service
@RequiredArgsConstructor
public class UserWechatRelationServiceImpl extends BaseServiceImpl<UserWechatRelationMapper, UserWechatRelationDO, UserWechatRelationResp, UserWechatRelationDetailResp, UserWechatRelationQuery, UserWechatRelationReq> implements UserWechatRelationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOpenId(List<Long> openIds) {
        baseMapper.lambdaUpdate().in(UserWechatRelationDO::getOpenId, openIds).remove();
    }

    @Override
    public boolean isUserExists(String openId) {
        return baseMapper.lambdaQuery().in(UserWechatRelationDO::getOpenId, openId).exists();
    }

    @Override
    public void bindUser(UserWechatRelationReq req) {
        var existsFlag = baseMapper.lambdaQuery().in(UserWechatRelationDO::getOpenId, req.getOpenId()).exists();
        if (!existsFlag) {
            //super.add(req);
            UserWechatRelationDO relationDO = new UserWechatRelationDO();
            relationDO.setUserId(req.getUserId());
            relationDO.setOpenId(req.getOpenId());
            relationDO.setCreateTime(LocalDateTime.now());

            baseMapper.insert(relationDO);
        }
    }

    @Override
    public boolean isRelationExists(UserWechatRelationReq req) {
        var result = baseMapper.lambdaQuery()
            .select(UserWechatRelationDO::getOpenId)
            .eq(UserWechatRelationDO::getUserId, req.getUserId())
            .list()
            .stream()
            .map(UserWechatRelationDO::getOpenId)
            .toList();

        return result.size() > 0;
    }
}